using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Text;
using System.Web;

using System.Web.Caching;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace ACM.cmsarea08.Controls
{
    [DefaultProperty("Text")]
    [ToolboxData("<{0}:GoogleMapInput runat=server></{0}:GoogleMapInput>")]
    public class GoogleMapInput : TextBox
    {

        [Bindable(true)]
        [Category("Appearance")]
        [DefaultValue("")]
        [Localizable(true)]
        public string GoogleMapKey
        {
            get
            {
            	
                object o = ViewState["GoogleMapKey"];
                if (o == null) {
                	if(System.Configuration.ConfigurationSettings.AppSettings["googleMapsKey"]!=null) 
                		return System.Configuration.ConfigurationSettings.AppSettings["googleMapsKey"];
                	else
                		return string.Empty;
                }
                else return o.ToString();
            }
            set
            {
                ViewState["GoogleMapKey"] = value;
            }
        }

        public GoogleMapInput()
		{
            

			
		}
		
        protected override void OnLoad(EventArgs e)
        {
            base.OnLoad(e);
            string embedobjecturl = "/template/plugins/GoogleMapInput/GoogleMapInput.js";//Page.ClientScript.GetWebResourceUrl(typeof(EmbedInput), "ACM.cmsarea08.Controls.Resources.EmbedObject.js");
            string cssembedobjecturl = "/template/plugins/GoogleMapInput/GoogleMapInput.css";//Page.ClientScript.GetWebResourceUrl(typeof(EmbedInput), "ACM.cmsarea08.Controls.Resources.EmbedObject.css");
           // HttpContext.Current.Response.Write("--> " + embedobjecturl);

            if(this.Page is ACM.cmsarea08.Core.MainPage) {
            	//((ACM.cmsarea08.Core.MainPage)this.Page).RegisterMooTools();
                ((ACM.cmsarea08.Core.MainPage)this.Page).RegisterCustomJavascript("/template/plugins/mootools-1.2-core.js");
                ((ACM.cmsarea08.Core.MainPage)this.Page).RegisterCustomJavascript("/template/plugins/mootools-1.2-more.js");
                ((ACM.cmsarea08.Core.MainPage)this.Page).RegisterCustomJavascript(embedobjecturl);
                ((ACM.cmsarea08.Core.MainPage)this.Page).RegisterCustomCss(cssembedobjecturl);
            } else {
            	//includi lo script... mootools..
            	if(this.Page.Header!=null) {
            		System.Web.UI.HtmlControls.HtmlHead myhead = this.Page.Header;
					bool foundMo = false;
                    bool foundMoMore = false;
                    bool foundEb = false;
                    bool foundEbc = false;
                    for (int i = 0; i < myhead.Controls.Count; i++)
                    {
                        if (myhead.Controls[i] is LiteralControl)
                        {
                            if (((LiteralControl)myhead.Controls[i]).Text.ToLower().IndexOf("mootools-1.2-core.js") != -1)
                            {
                                foundMo = true;
                            }
                            if (((LiteralControl)myhead.Controls[i]).Text.ToLower().IndexOf("mootools-1.2-more.js") != -1)
                            {
                                foundMoMore = true;
                            }

                            if (((LiteralControl)myhead.Controls[i]).Text.ToLower().IndexOf("embedobject.js") != -1)
                            {
                                foundEb = true;
                            }
                            if (((LiteralControl)myhead.Controls[i]).Text.ToLower().IndexOf("embedobject.css") != -1)
                            {
                                foundEbc = true;
                            }

                        }
                    }
                    if (!foundMo)
                    {
                        LiteralControl ct = new LiteralControl("<script language=\"javascript\" type=\"text/javascript\" src=\"/template/plugins/mootools-1.2-core.js\"></script>");
                        myhead.Controls.Add(ct);
                    }
                    if (!foundMoMore)
                    {
                        LiteralControl ct = new LiteralControl("<script language=\"javascript\" type=\"text/javascript\" src=\"/template/plugins/mootools-1.2-more.js\"></script>");
                        myhead.Controls.Add(ct);
                    }              		
					if(!foundEb) {
						LiteralControl ct = new LiteralControl("<script language=\"javascript\" type=\"text/javascript\" src=\""+embedobjecturl+"\"></script>");
						myhead.Controls.Add(ct);			
					}
                    if (!foundEbc)
                    {
                        LiteralControl ct = new LiteralControl("<style type=\"text/css\" media=\"screen\">\n"+
                            "<!--\n"+
                            "@import url(\""+cssembedobjecturl+"\");\n"+
                            "-->\n</style>");
                        myhead.Controls.Add(ct);
                    }             		
            		
            	}
            }
        }		
        protected override void Render(HtmlTextWriter writer)
        {
            
			//this.ID
			//this.ClientID
			//this.UniqueID
            string thisContainer = this.ClientID + "CC";
            string valore = this.Text;
            string lat = "";
            string log = "";
            if (!string.IsNullOrEmpty(valore) && valore.Trim() != "")
            {
                if (valore.IndexOf(',') != -1)
                {
                    lat = valore.Trim().Substring(0, valore.IndexOf(','));
                    log = valore.Trim().Substring(valore.IndexOf(',') + 1);
                }
            }


 writer.Write("<div id=\""+thisContainer+"\" >");
 writer.Write("<div class=\"ginputarea\">");
 writer.Write("<fieldset>");
 writer.Write("<legend>Localit&agrave;</legend>");
 writer.Write("<label for=\""+thisContainer+"_inputAdd\">Localit&agrave;</label>");
 writer.Write("<input type=\"text\" id=\""+thisContainer+"_inputAdd\" />  ");
 writer.Write("<input type=\"button\" id=\""+thisContainer+"_btmAdd\" value=\"update\" class=\"btmgupd\" />");
 writer.Write("</fieldset>");
 writer.Write("<fieldset class=\"latlong\">");
 writer.Write("<legend>Coordinate</legend>");
 writer.Write("<label for=\""+thisContainer+"_inputLA\">Lat.</label>");
 writer.Write("<input type=\"text\" id=\""+thisContainer+"_inputLA\" /> ");
 writer.Write("<label for=\""+thisContainer+"_inputLO\">Long.</label>");
 writer.Write("<input type=\"text\" id=\""+thisContainer+"_inputLO\" />       ");                     
 writer.Write("<input type=\"button\" id=\""+thisContainer+"_btmLL\" value=\"update\" class=\"btmgupd\" />   ");             
 writer.Write("</fieldset>");
 writer.Write("</div>");
 writer.Write("<div class=\"gmaparea\">");
 writer.Write("<div id=\"" + thisContainer + "_maps\" style=\"width:240px; height:135px; border:1px solid #333; \">");
 writer.Write("</div>");
 writer.Write("</div>");
 writer.Write("<input type=\"hidden\" value=\"" + this.Text + "\" id=\"" + this.ClientID + "\" name=\"" + this.UniqueID + "\" />");
 writer.Write("</div>");
            string gK = this.GoogleMapKey;
            string lato = "";
            if (lat.Trim() != "" && log.Trim() != "")
            {
                lato = ",latitude:'" + lat + "',longitude:'" + log + "'";
            }
		    writer.Write("<script language=\"javascript\" type=\"text/javascript\" defer=\"defer\">\n");
            writer.Write("var " + this.ID + "imoEl = new GoogleMapsObj('','','',{panelID:'" + thisContainer + "',  " +
                                                "googlekey:'" + gK + "',dataID:'" + this.ClientID + "'" + lato + "});	\n");

            writer.Write("function mappa(puntoL,puntoG) {\n");
            writer.Write("" + this.ID + "imoEl.setLatLong(puntoL,puntoG);\n");
            writer.Write("}\n");
		    writer.Write("</script>\n");
		    
		    
		    
		    
		    

        }
        
	}
}
